#include "stdafx.h"
#include "ckFolder.h"
#include "ckEnumFolder.h"

namespace ck
{
	Folder::Folder()
	{
	}


	Folder::~Folder()
	{
	}

	const char* Folder::GetName()
	{
		return m_strName.c_str();
	}

	const char* Folder::GetPath()
	{
		return m_strPath.c_str();
	}

	void Folder::SetName(const char* szName)
	{
		if (szName)
		{
			m_strName = szName;
		}
		else
		{
			m_strName.clear();
		}
	}

	void Folder::SetPath(const char* szPath)
	{
		if (szPath)
		{
			m_strPath = szPath;
		}
		else
		{
			m_strPath.clear();
		}
	}

	EnumFolder*	Folder::GetSubFolders()
	{
		char szFilter[Z_PATH_MAX];
		memset(szFilter, 0, Z_PATH_MAX);
		_makepath(szFilter, NULL, m_strPath.c_str(), "*", "*");

		EnumFolder* pFolders = new EnumFolder();

		WIN32_FIND_DATAA fd;
		HANDLE handle = FindFirstFileA(szFilter, &fd);
		if (handle != INVALID_HANDLE_VALUE)
		{
			while (FindNextFile(handle, &fd))
			{
				if (fd.cFileName[0] == '.')
				{
					continue;
				}
				else
				{ 
					char szSubPath[Z_PATH_MAX];
					if (fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
					{
						_makepath(szSubPath, NULL, m_strPath.c_str(), fd.cAlternateFileName, NULL);
						Folder* pFolder = new Folder();
						pFolder->SetName(fd.cFileName);
						pFolder->SetPath(szSubPath);
						pFolders->Add(pFolder);
					}
				}
			}
		}

		return pFolders;
	}

}
